package test.cei.tallerdda.socket.cliente;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Cliente {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String mensaje = null;
        Socket socket = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            do {
                InetAddress host = InetAddress.getLocalHost();
                socket = new Socket(host.getHostName(), 9999);
                mensaje = new Scanner(System.in).nextLine();
                oos = new ObjectOutputStream(socket.getOutputStream());
                oos.writeObject(mensaje);
                ois = new ObjectInputStream(socket.getInputStream());
                String respuesta = (String) ois.readObject();
                System.out.println("Message: " + respuesta);
                ois.close();
                oos.close();
                socket.close();
            } while (!mensaje.equals("salir"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
                if (oos != null) {
                    oos.close();
                }
                if (ois != null) {
                    socket.close();
                }
            } catch (Exception e) {
                System.out.println("Ok, más no puedo hacer");
            }
        }
    }
}
